<?php
require 'db.php';

$id = isset($_GET['id'])?$_GET['id']:0;
$sbmtBtn = "Add me!!";
$where = '';

////////// Prepare seaarch Query //////////
if(isset($_POST['searchQuery']) && !empty($_POST['searchQuery'])){
  $searchInput = trim($_POST['searchQuery']);
  $where = " WHERE `name` LIKE '%$searchInput%'";
}
///////////////////////////////////////////
pr($_POST);
if(isset($_GET['mode']) && $_GET['mode']=='edit'){
  $selQuery = "SELECT * FROM `countries` WHERE `id`='$id'";
  $selResult = mysql_query($selQuery) or die(mysql_error());
  $ansCountry = mysql_fetch_assoc($selResult);
 // pr($ansCountry);
  $sbmtBtn = "Update me!!";

}

if(isset($_GET['mode']) && $_GET['mode']=='delete'){

  $delQuery = "DELETE FROM `countries` WHERE `id`='$id'";
  mysql_query($delQuery);
  $deletedRecords = mysql_affected_rows();
  echo "<h1 style='color:green'>$deletedRecords country deleted</h1>";
}
//$_POST['id']!=0 shows that we are UPDATING
if(isset($_POST['name']) && !empty($_POST['name']) && $_POST['id']!=0){
  $id = $_POST['id'];
  $name = $_POST['name'];

  $updateQuery = "UPDATE `countries` SET `name`='$name' WHERE `id`='$id' ";
  mysql_query($updateQuery) or die(mysql_error());
  if(mysql_affected_rows() > 0){
    echo "<h4>Country Updated</h4>";
    $id = 0;
    $sbmtBtn = "Add me!!";
    $ansCountry['name'] = '';
  }
}


//$_POST['id']==0 shows that we are inserting
if(isset($_POST['name']) && !empty($_POST['name']) && $_POST['id']==0){
  $name = $_POST['name'];
  $insert = "INSERT INTO `countries`
     (`name`)VALUES('$name');";

mysql_query($insert) or die(mysql_error());

$rowsInserted =  mysql_affected_rows();
$lastId = mysql_insert_id();

echo "<p>$rowsInserted country added on Id:$lastId<p>";
}

/////////  Paging Start //////////////////////////
$totalQuery = "SELECT COUNT(*) as `total` FROM `countries` $where";
$totalResult = mysql_query($totalQuery)or die(mysql_error());
$totalAns = mysql_fetch_assoc($totalResult);
$total_records = $totalAns['total'];

$page_num = isset($_GET['page'])?$_GET['page']:1;
$record_per_page = 4;
$total_pages = ceil($total_records/$record_per_page);
$index = ($page_num-1)*$record_per_page;

//////////////////////////////////////////////////


//////////////////////////////////////////////////
$select = "SELECT * FROM `countries` $where  ORDER BY `id` LIMIT $index,$record_per_page";
pr($select);
$result = mysql_query($select) or die(mysql_error());

echo "<h4>$total_records Countries Selected</h4>";
//////////////////////////////////////////////////
?>
<form action="" method="post" >
  <input type="hidden" name="id" value="<?=$id?>">
  <table width="40%" align="center" border="1">
    <thead>
      <tr>
        <th colspan="2">Add Country</th>

      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Name</td>
        <td><input type="text"
value="<?=@$ansCountry['name']?>"
         name="name"></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td><input type="submit" value="<?=$sbmtBtn?>" /></td>
      </tr>
    </tbody>
  </table>
</form>

<form name="searchForm" action="" method="post">
  <table width="40%" align="center" border="1">
      <tr>
        <td><input type="search" required name="searchQuery" ><input type="submit" value="Search"></td>
      </tr>
  </table>
</form>

  <table width="40%" align="center" border="1">
  <thead>
    <tr>
      <th>#</th>
      <th>Country</th>
      <th >Options</th>
    </tr>
  </thead>
  <tbody>
<?php
$i=1;
while($ans = mysql_fetch_assoc($result)){
?>
    <tr>
      <td><?=$ans['id']?></td>
      <td><?=$ans['name']?></td>
      <td>
<!-- Delete -->
<a href="add_country.php?id=<?=$ans['id']?>&mode=delete" onClick="return confirm('Are you sure ?')">Delete</a>
<!--Edit -->
<a href="add_country.php?id=<?=$ans['id']?>&mode=edit">Edit</a>

</td>
    </tr>
<?php
}//end while
?>
  </tbody>
</table>


  <table width="40%" align="center" border="1">
      <tr>
<?php for($i=1 ; $i<=$total_pages ; $i++){?>
        <td><a href="add_country.php?page=<?=$i?>"><?=$i?></a></td>
<?php }?>
      </tr>
  </table>
